/*******************************************************************************
 * * Copyright 2012 Impetus Infotech.
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 ******************************************************************************/
package cn.ilikes.tools.hbase.jpa.metadatas.processor;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;

import cn.ilikes.tools.hbase.jpa.annotations.IndexColumn;
import cn.ilikes.tools.hbase.jpa.annotations.Save;
import cn.ilikes.tools.hbase.jpa.metadatas.IEntityMetaData;
import cn.ilikes.tools.hbase.jpa.metadatas.elements.EntityFieldsImpl;
import cn.ilikes.tools.hbase.jpa.metadatas.elements.IndexField;
import cn.ilikes.tools.hbase.jpa.metadatas.processor.processores.ElementProcessorException;
import cn.ilikes.tools.hbase.jpa.metadatas.processor.processores.EntityFieldElementProcessors;
import cn.ilikes.tools.hbase.jpa.metadatas.processor.processores.EntityTableProcessors;
import cn.ilikes.tools.hbase.jpa.metadatas.processor.processores.Processors;
import cn.ilikes.tools.hbase.jpa.metadatas.processor.validators.EntityValidatorException;
import cn.ilikes.tools.logs.Loggerfactory;

public class TableProcessor extends AbstractEntityProcessor {

	public static void processorsIndexColumn(EntityFieldsImpl entityFields, Field f) {
		IndexColumn indexColumn = f.getAnnotation(IndexColumn.class);
		if (indexColumn == null)return;
		IndexField indexField = new IndexField();
		indexField.setName((indexColumn.name().isEmpty() ? f.getName() : indexColumn.name()));
		indexField.setIndex(indexColumn.index());
		indexField.setBoost(indexColumn.boost());
		indexField.setStore(indexColumn.store());
		indexField.setTermVector(indexColumn.termvector());
		entityFields.setIndexField(indexField);
		

	}

	private Logger logger = Loggerfactory.instance(TableProcessor.class);

	private static List<Processors> entityProcessors = new ArrayList<Processors>();
	static {
		entityProcessors.add(new EntityTableProcessors());

		entityProcessors.add(new EntityFieldElementProcessors());
	}

	@Override
	public void process(Class<?> clazz, IEntityMetaData metadata) throws TableProcessorException {
		Loggerfactory.info(logger, "start  processor table entity [ " + clazz.getSimpleName() + " ] ");

		for (Processors processors : entityProcessors) {
			try {
				processors.processors(clazz, metadata);
			} catch (ElementProcessorException e) {
				Loggerfactory.error(logger, e);
				throw new TableProcessorException(e);
			}

		}

	}

	@Override
	public boolean entityValidator(Class<?> clazz, IEntityMetaData metadata) {

		for (Processors processors : entityProcessors) {
			try {
				processors.validators(clazz, metadata);
			} catch (EntityValidatorException e) {
				Loggerfactory.error(logger, e);
				return false;
			}
		}
		return true;
	}

}
